<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1037329
-->
<head>
  <meta charset="utf-8">
  <title>System Update API Test</title>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1037329">Test System Update API</a>
<script type="application/javascript;version=1.8">

'use strict';

SimpleTest.waitForExplicitFinish();

function setup() {
  window.gUrl = SimpleTest.getTestFileURL('preload-SystemUpdateManager-jsm.js');
  window.gScript = SpecialPowers.loadChromeScript(gUrl);
  return Promise.resolve();
}

function testGetProviders() {
  return new Promise(function(resolve, reject) {
    navigator.updateManager.getProviders().then(function(providerInfos) {
      info('num of providers: ' + providerInfos.length);
      for (let providerInfo of providerInfos) {
        info('provider info: ' + JSON.stringify(providerInfo));
      }
      resolve(providerInfos);
    });
  });
}

function testSetActiveProvider(providerInfos) {
  return new Promise(function(resolve, reject) {
    //Find the mock provider for our testing provider instead.
    //Set the mock provider as active provider.
    let targetProvider = providerInfos[0];
    for(let provider of providerInfos) {
      if(provider.uuid == "{17a84227-28f4-453d-9b80-9ae75a5682e0}") {
        info('target provider uuid: ' + provider.uuid);
        targetProvider = provider;
        break;
      }
    }
    is("{17a84227-28f4-453d-9b80-9ae75a5682e0}", targetProvider.uuid, 'get the dynamically added provider');
    navigator.updateManager.setActiveProvider(targetProvider.uuid).then(function(activeProvider) {
      info('active provider info: ' + JSON.stringify(activeProvider.info));
      is(activeProvider.name, targetProvider.name, 'expected name of active provider');
      is(activeProvider.uuid, targetProvider.uuid, 'expected uuid of active provider');
      resolve({name : activeProvider.name, uuid : activeProvider.uuid});
    });
  });
}

function testGetActiveProvider(providerInfo) {
  info('test GetActiveProvider');
  return new Promise(function(resolve, reject) {
    navigator.updateManager.getActiveProvider().then(function(activeProvider) {
      is(activeProvider.name, providerInfo.name, 'expected name of active provider');
      is(activeProvider.uuid, providerInfo.uuid, 'expected uuid of active provider');
      resolve(activeProvider);
    });
  });
}

function testCheckForUpdate(provider) {
  info('test CheckForUpdate');
  return new Promise(function(resolve, reject) {
    provider.addEventListener('updateavailable', function(event) {
      ok(true, 'receive updateavailable event');
      info('event: ' + JSON.stringify(event.detail));
      resolve(provider);
    });
    provider.checkForUpdate();
  });
}

function testStartDownload(provider) {
  info('test StartDownload');
  return new Promise(function(resolve, reject) {
    provider.addEventListener('progress', function(event) {
      ok(true, 'receive progress event');
      is(event.loaded, 10, 'expected loaded');
      is(event.total, 100, 'expected total');
      resolve(provider);
    });
    provider.startDownload();
  });
}
function testStopDownload(provider) {
  info('test StopDownload');
  return new Promise(function(resolve, reject) {
    provider.stopDownload();
    resolve(provider);
  });
}
function testApplyUpdate(provider) {
  info('test ApplyUpdate');
  return new Promise(function(resolve, reject) {
    provider.applyUpdate();
    resolve(provider);
  });
}
function testGetParameter(provider) {
  info('test GetParameter');
  return new Promise(function(resolve, reject) {
    let dummy = provider.getParameter('dummy');
    is(dummy, 'dummy-value', 'expected parameter');
    resolve(provider);
  });
}
function testSetParameter(provider) {
  info('test SetParameter');
  return new Promise(function(resolve, reject) {
    provider.setParameter('dummy', 'dummy-value');
    resolve();
  });
}
function testSetActiveProviderError() {
  info('test setActiveProvider error');
  return new Promise(function(resolve, reject) {
    navigator.updateManager.setActiveProvider('something not exsited').then(function(provider) {
      ok(false, 'should not success');
      resolve();
    }, function(reason) {
      info('error message: ' + reason);
      ok(true, 'expected error while setActiveProvider');
      resolve();
    });
  });
}


function runTest() {
  ok(navigator.updateManager, 'should have navigator.updateManager');

  setup()
  .then(testGetProviders)
  .then(testSetActiveProvider)
  .then(testGetActiveProvider)
  .then(testCheckForUpdate)
  .then(testStartDownload)
  .then(testStopDownload)
  .then(testApplyUpdate)
  .then(testGetParameter)
  .then(testSetParameter)
  .then(testSetActiveProviderError)
  .then(function() {
    info('test finished');
    gScript.destroy();
    SimpleTest.finish();
  });
}

SpecialPowers.pushPermissions([
    {type: 'system-update', allow: true, context: document},
  ], function() {
    SpecialPowers.pushPrefEnv({
        'set': [
          ['dom.system_update.enabled', true],
          ['dom.system_update.debug', true],
          ['dom.system_update.active', '@mozilla.org/test-update-provider;1'],
        ]
      }, runTest);
  }
);
</script>
</pre>
</body>
</html>
